package com.yang.leather.admin.action.prod;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.yang.leather.admin.action.common.BaseUploadAction;
import com.yang.leather.admin.pojo.Language;
import com.yang.leather.admin.pojo.ProdType;
import com.yang.leather.admin.pojo.Product;
import com.yang.leather.admin.service.IProdService;
import com.yang.leather.admin.service.IProdTypeService;
import com.yang.leather.common.page.Pagination;
import com.yang.leather.common.struts2.ContextPvd;
import com.yang.leather.common.upload.IUploadService;
import com.yang.leather.common.upload.ImageUploadType;
import com.yang.leather.common.upload.UploadException;
import com.yang.leather.common.util.RequestUtil;

@Scope("prototype")
@Controller("admin.prod")
public class ProdAction extends BaseUploadAction{

	private static final long serialVersionUID = 1L;
	
	private IProdService prodService;
	private ContextPvd contextPvd;
	private IUploadService uploadService;
	private IProdTypeService prodTypeService;

	private Product product;
	
	public String list() {
		Map<String,String> param = RequestUtil.getMapByRequest(this.getRequest());
		int pageNo = 1;
		if(StringUtils.isNotEmpty(param.get("curPage"))){
			pageNo = Integer.parseInt(param.get("curPage"));
		}
		int limit = 10;
		if(StringUtils.isNotEmpty(param.get("perPage"))){
			limit = Integer.parseInt(param.get("perPage"));
		}
		
		Pagination page = prodService.getProductListPage(param, pageNo, limit);
		this.getRequest().setAttribute("page", page);
		
		List<ProdType> types = this.prodTypeService.getAllProdTypes();
		this.getRequest().setAttribute("types", types);
		
		this.getRequest().setAttribute("languages", Language.getLanguage());
		
		//商品类型
		this.getRequest().setAttribute("req_typeId", param.get("typeId"));
		//商品类型名称
		this.getRequest().setAttribute("req_typeName", param.get("typeName"));
		//商品名称
		this.getRequest().setAttribute("req_prodName", param.get("prodName"));
		//商品编码
		this.getRequest().setAttribute("req_prodCode", param.get("prodCode"));
		//商品状态
		this.getRequest().setAttribute("req_status", param.get("status"));
		
		return SUCCESS;
	}
	
	private void setEditPro(){
		List<ProdType> types = this.prodTypeService.getAllProdTypes();
		this.getRequest().setAttribute("types", types);
		
		this.getRequest().setAttribute("languages", Language.getLanguage());
	}
	
	//请求商品添加页面
	public String toAdd() {
		//设置上传规则
		addUploadRule();
		
		setEditPro();
		
		return SUCCESS;
	}
	
	public String uploadImg() {
		String uploadNum = this.getRequest().getParameter("uploadNum");
		try{
			String filePath = uploadService.uploadProduct(this.getRequest(), "uploadFile",this.getRequest().getParameter("uploadRuleId"));
		    
		    
		    renderHtmlGBK("<html><script type=\"text/javascript\">setTimeout(function(){parent.$('#uploadFile_span"+uploadNum+"').unblock();parent.Upload.getImgSrc('0','" + filePath + "','" +uploadNum + "');},500);</script></html>");
		}catch(UploadException e){
			String code = "1";
			if(IUploadService.APPLICATION_ERROR.equals(e.getType())){
				code = "3";
			}else if(IUploadService.FILE_SIZE_ERROR.equals(e.getType())){
				code = "1";
			}
			renderHtmlGBK("<html><script type=\"text/javascript\">setTimeout(function(){parent.$('#uploadFile_span"+uploadNum+"').unblock();parent.Upload.getImgSrc('" +code+ "');},500);</script></html>");
		}
		return null;
	}
	
	public String toEdit(){
		view();
		
		return SUCCESS;
	}
	
	private void view(){
		String prodId = this.getRequest().getParameter("id");
		
		product = this.prodService.findById(Long.valueOf(prodId));
		
		setEditPro();
		
		// 设置上传规则
		this.addUploadRuleBeforeUpd(product.getProdDesc());
	}
	
	public String save(){
		this.prodService.saveProduct(product);
		
		return SUCCESS;
	}
	
	public String edit(){
		this.prodService.updateProduct(product);
		
		return SUCCESS;
	}
	
	public String del(){
		String sid = this.getRequest().getParameter("id");
		
		prodService.deleteById(Long.valueOf(sid));
		
		renderText("1");
		
		return null;
	}
	
	public String batchDel(){
		String ids = this.getRequest().getParameter("ids");
		List<Long> list = new ArrayList<Long>();
		for(String sId : ids.split(",")){
			list.add(Long.valueOf(sId));
		}
		prodService.batchDel(list);
		
		renderText("1");
		
		return null;
	}
	
	//验证编码是否存在
	public String validProdCode(){
		String prodCode = this.getRequest().getParameter("prodCode");
		if(StringUtils.isNotEmpty(prodCode)){
			Map<String,String> param = new HashMap<String,String>();
			param.put("prodCode", prodCode);
			
			int count = this.prodService.countProductList(param);
			this.renderText("{\"count\":" +count+ "}");
		}
		
		return null;
	}
	
	public String updStatus(){
		String status = this.getRequest().getParameter("status");
		
		String sids = this.getRequest().getParameter("ids");
		
		if(StringUtils.isNotEmpty(sids)){
			List<Long> lstId = new ArrayList<Long>();
			for(String id : sids.split(",")){
				lstId.add(Long.valueOf(id));
			}
			this.prodService.updateStatus(Integer.valueOf(status),lstId);
		}
		
		renderText("1");
		return null;
	}

	public void addUploadType() {
		this.setUploadType(ImageUploadType.PROD_DETAIL.getType());
	}

	public IProdService getProdService() {
		return prodService;
	}

	@Autowired
	public void setProdService(@Qualifier("prodService")
			IProdService prodService) {
		this.prodService = prodService;
	}

	public ContextPvd getContextPvd() {
		return contextPvd;
	}

	@Autowired
	public void setContextPvd(@Qualifier("contextPvd")
			ContextPvd contextPvd) {
		this.contextPvd = contextPvd;
	}

	public IUploadService getUploadService() {
		return uploadService;
	}

	@Autowired
	public void setUploadService(@Qualifier("uploadService")
			IUploadService uploadService) {
		this.uploadService = uploadService;
	}

	public IProdTypeService getProdTypeService() {
		return prodTypeService;
	}

	@Autowired
	public void setProdTypeService(@Qualifier("prodTypeService")
			IProdTypeService prodTypeService) {
		this.prodTypeService = prodTypeService;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}
}
